//
//  JWTabBarButton.m
//  JWMovie
//
//  Created by Jorn.Wu on 15/7/17.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "JWTabBarButton.h"

@interface JWTabBarButton()
@property (nonatomic, strong) UIImageView *mImageView;
@property (nonatomic, strong) UILabel *mTitle;

@end

@implementation JWTabBarButton

- (instancetype)initWithIcon:(NSString *)name title:(NSString *)title{
    if (self = [super init]) {
        self.icon = [UIImage imageNamed:name];
        self.title = title;
        
        _mImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _mImageView.contentMode = UIViewContentModeScaleAspectFit;
        _mImageView.image = self.icon;
        
        _mTitle = [[UILabel alloc] initWithFrame:CGRectZero];
        _mTitle.font = [UIFont systemFontOfSize:13];
        _mTitle.text = self.title;
        _mTitle.textColor = [UIColor grayColor];
        _mTitle.textAlignment = NSTextAlignmentCenter;
        
        [self addSubview:_mImageView];
        [self addSubview:_mTitle];
    }
    return self;
}

+ (instancetype)tabBarButtonWithIcon:(NSString *)name title:(NSString *)title{
    return [[self alloc] initWithIcon:name title:title];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat btnH = CGRectGetHeight(self.frame);
    CGFloat btnW = CGRectGetWidth(self.frame);
    CGFloat imageW = btnW * 0.6;
    CGFloat imageH = btnH * 0.6;
    CGFloat titleH = btnH * 0.4;
    CGFloat titleW = btnW * 0.6;
    
    _mImageView.frame = CGRectMake((btnW - imageW) * 0.5, 0, imageW, imageH);
    _mTitle.frame = CGRectMake((btnW - titleW) * 0.5, imageH + 1, titleW, titleH);
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
